﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace SpiderHelper.Tools.StockStar
{
    public class StockStarVisiter
    {
        public List<Model.TempDoc> ReadStockStar(Model.CollectionChannel channel, DavidDB.ADO.DBManager db)
        {
            List<Model.Filter> filters = Model.Filter.GetList();
            List<Model.TempDoc> docs = new List<Model.TempDoc>();
            System.Net.CookieContainer container = new System.Net.CookieContainer();
            Common.Net.Request request = new Common.Net.Request(container);
            string source = "";

            try
            {
                System.Threading.Thread.Sleep(1000);
                source = request.ReadUrlContent(channel.ChannelURL, channel.Encode);
            }
            catch (Exception)
            {
                //   source.Debug();
            }
            if (!string.IsNullOrEmpty(source))
            {
                if (!channel.ChannelName.Equals("图文天下"))//文字
                {
                    docs = GetDocsFromStockStarHtml(source, channel);
                }
                else//图片
                {

                }
            }
            else
            {
                (channel.ChannelName + "  " + channel.ChannelURL + "  解析失败").Debug();
            }
            return docs;
        }

        private List<Model.TempDoc> GetDocsFromStockStarHtml(string html, Model.CollectionChannel channel)
        {
            List<Model.TempDoc> docs = new List<Model.TempDoc>();
            //  List<string> docstrings = new List<string>();
            html = html.Substring(html.IndexOf("<!--列表页-->") + 10);
            int endindex = html.IndexOf("<!--end 列表页-->");
            if (endindex > 0)
            {
                html = html.Substring(0, html.IndexOf("<!--end 列表页-->"));
            }
            else
            {
                return docs;
            }


            Regex reg = new Regex(@"<a [\s\S]*?>[\s\S]*?</a>", RegexOptions.IgnoreCase);
            MatchCollection mc = reg.Matches(html);
            foreach (Match match in mc)
            {
                string s = match.Value.ToString();
                Model.TempDoc doc = new Model.TempDoc();
                doc.Author = "";
                doc.ChannelId = 2;
                doc.Pdate = DateTime.Now;
                doc.Description = "";
                doc.Link = s.Substring(0, s.IndexOf(">"));
                doc.Link = doc.Link.Substring(doc.Link.IndexOf("\"") + 1);
                doc.Link = doc.Link.Substring(0, doc.Link.IndexOf("\""));
                s = s.Substring(s.IndexOf(">") + 1);
                doc.Title = s.Substring(0, s.IndexOf("<"));
                bool sured = false;
                doc.Content = new StockStarURLAnalysis().AnalysisURLContent(doc.Link, channel.Encode, channel.Id, out sured);
                doc.Sured = sured == true ? 1 : 0;
                doc.SiteId = channel.Id;
                docs.Add(doc);
            }

            return docs;
        }

        private List<Model.TempDoc> GetPicsAlbumsFromStockStarHtml(string html)
        {
            List<Model.TempDoc> docs = new List<Model.TempDoc>();



            return docs;
        }
    }
}
